<?php

namespace App\Http\Controllers\web;

use App\Http\Controllers\Controller;
use App\Service\product\ProductElseService;
use App\Service\product\ProductStatisticsService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use lxs\api\ApiResponse;

class StatisticsController extends Controller
{

    /**
     * 获取数据中心（产品数据）页面数据
     */
    public function getDataCenterData(Request $request): JsonResponse
    {

        list($uid, $post, $productData, $searchData) = $this->extracted($request);
        $result = ProductStatisticsService::instance()->getProductStatisticsData($uid, $post['page'], $post['pageSize'], true, $searchData);

        return ApiResponse::apiResponseData(200, '获取数据中心页面数据成功', [
            'product_data' => $productData,
            'lists' => $result['lists'],
            'page' => $result['page'],
            'page_size' => $result['page_size'],
            'total' => $result['total']
        ]);
    }


    /**
     * 获取数据中心（收益数据）页面数据
     */
    public function getDataCenterIncomeData(Request $request): JsonResponse
    {

        list($uid, $post, $productData, $searchData) = $this->extracted($request);

        $result = ProductStatisticsService::instance()->getProductIncomeStatisticsData($uid, $post['page'], $post['pageSize'], $searchData);

        return ApiResponse::apiResponseData(200, '获取数据中心页面数据成功', [
            'product_data' => $productData,
            'statistics' => $result['statistics'],
            'lists' => $result['lists'],
        ]);
    }

    /**
     * @param Request $request
     * @return array
     */
    public function extracted(Request $request): array
    {
        $uid = $request->web_uid;
        $post = $request->input();

        $productData = ProductElseService::getProductTitleList();

        $searchData = [];
        if (isset($post['product_title']) && $post['product_title'] != '') {
            $searchData['product_title'] = $post['product_title'];
        }
        if (isset($post['data_time'])) {
            $searchData['data_time'] = $post['data_time'];
        }
        return array($uid, $post, $productData, $searchData);
    }


}
